package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;

import com.smarthealthring.mm.R;

import java.math.BigDecimal;

import static android.text.InputType.TYPE_CLASS_NUMBER;
import static android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL;

/**
 * Created by sun on 17-11-17.
 * 满足一定规则的EditText
 */

public class DecNumEditText extends AppCompatEditText implements CheckStatusTextView.ValidStatusObserable {

    private int maxIntLen = 10;//整数长度
    private int maxDecLen = 2;//小数长度

    public DecNumEditText(Context context) {
        super(context);
        init();
    }

    public DecNumEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        this.setInputType(TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL);
        this.setGravity(Gravity.RIGHT);
        this.setBackgroundDrawable(null);
        this.setTextColor(getContext().getResources().getColor(R.color.text_black));
        this.setHintTextColor(getContext().getResources().getColor(R.color.text_gray));
        this.setTextSize(14);
        this.setOnFocusChangeListener((view, b) -> {
            if (!b) {
                format();
            }
        });
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence sequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                format(DecNumEditText.this, editable);
            }
        });
    }

    /**
     * 设置整数部分小数部分最大位数
     *
     * @param intLen
     * @param decLen
     */
    public void setLength(int intLen, int decLen) {
        this.maxIntLen = intLen;
        this.maxDecLen = decLen;
    }

    private void format(EditText et, Editable s) {
        String temp = et.getText().toString();
        //第一位不可为0
        if (temp.contains("0") && temp.indexOf("0") == 0) {
            if (temp.length() > 1 && !temp.contains("."))
                s.delete(0, 1);
        }
        //最多两位小数
        if (temp.contains(".")) {
            int posDot = temp.indexOf(".");
            if (posDot < 0) return;
            if (posDot == 0) s.clear();
            if (temp.length() - posDot - 1 > maxDecLen) {
                s.delete(posDot + maxDecLen + 1, posDot + maxDecLen + 2);
            }
        } else {//最多10位整数
            if (temp.length() > maxIntLen) {
                s.delete(maxIntLen, maxIntLen + 1);
            }
        }
        if (mListener != null) {
            mListener.status(isValid());
        }
    }

    private void format() {
        //TODO
//        if (TextUtils.isEmpty(getText().toString())) {
//            setText("0");
//        }
//        BigDecimal bd = new BigDecimal(getText().toString());
//        setText(String.format("%.2f", bd));
    }

    private CheckStatusTextView.StatusListener mListener;

    @Override
    public void setListener(CheckStatusTextView.StatusListener listener) {
        this.mListener = listener;
    }

    @Override
    public boolean isValid() {
        if (TextUtils.isEmpty(getText().toString())) {
            boolean isHintValid;
            try {
                BigDecimal bd = new BigDecimal(getHint().toString());
                isHintValid = true;
            } catch (NumberFormatException e) {
                isHintValid = false;
            }
            return isHintValid;
        }
        return true;
    }
}
